#ifndef FILETRANSFER_DOWNLOADER_H
#define FILETRANSFER_DOWNLOADER_H

#include "urlextracter.h"

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QFile>
#include <QBuffer>


namespace FileTransfer {

class DownLoader : public QObject
{
    Q_OBJECT

public:
    DownLoader(QObject *parent = 0);
    ~DownLoader();

private:
    QNetworkAccessManager *m_manager;
    QNetworkReply *m_reply;
    QFile *m_file;
    QBuffer *m_buffer;
    QString m_fileName;
    QString m_path;
    bool m_isAborted;
    bool m_isPaused;
    QString m_errorString;
    UrlExtracter *m_extracter;
    QString m_folder;
    QString m_fileUrl;
    bool m_overwriteFiles;
    QString m_redirectLocation;
    bool m_isCache;
    bool m_isBuffer;
    QString m_tempExtension;

Q_SIGNALS:
    void started(const QString &fileName, qint64 size, const QString &fileUrl);
    void progress(qint64 bytesReceived, qint64 bytesTotal);
    void finished(const QString &error, bool aborted, bool paused);

private Q_SLOTS:
    void onHeadersChanged();
    void onDataReady();
    void onRequestFinished();
    void onDataCodecUrlReceived(const QString &urlString);

public Q_SLOTS:
    void download(const QString &urlString,
                  bool overwrite = true,
                  const QString &folder = "");
    void downloadFromDataCodec(const QString &urlString, const QString &folder, bool overwrite);
    void cancelDownload();
    void pauseDownload();

public:
    inline const QString &path() const {
        return m_path;
    }

    inline const QString &fileName() const {
        return m_fileName;
    }
};

} // namespace FileTransfer

#endif // FILETRANSFER_DOWNLOADER_H
